﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "CommandBase.h"
#include "IUiDocumentViewManager.h"
#include "IUiDocument.h"
#include "IDocument.h"
#include "GDFoundation/Command.h"
#include "GDFoundation/EditCommandIds.h"
#include "ICommandManager.h"
#include "CommandParameters.h"

using namespace gcmp;
namespace Sample
{
    namespace CommandId
    {
        constexpr auto ProjectStyle = L"Sample.CommandId.ProjectStyle";
    }

    class ProjectionStyleCommand :public  gcmp::CommandBase
    {
    public:
        ProjectionStyleCommand()
            :CommandBase(Sample::CommandId::ProjectStyle) {};
    private:
        bool IsEnabled() const override;
        bool IsVisible() const override;
        bool ShouldPopExistingActionsBeforeExecution() const override { return false; }
        bool ShouldClearSelectionBeforeExecution() const override { return false; }
        gcmp::OwnerPtr<gcmp::IAction> ExecuteCommand(const gcmp::CommandParameters& cmdParams) override;
    private:
        gcmp::IDocument* GetDocument() const;
    };

    bool ProjectionStyleCommand::IsEnabled() const
    {
        gcmp::IDocument* pDocument = GetDocument();
        return pDocument != nullptr;
    }

    bool ProjectionStyleCommand::IsVisible() const
    {
        gcmp::IDocument* pDocument = GetDocument();
        return pDocument != nullptr;
    }

    gcmp::OwnerPtr<gcmp::IAction> ProjectionStyleCommand::ExecuteCommand(const gcmp::CommandParameters& cmdParams)
    {
        //commandParams 可传入两个参数，ParamType 与 ElementIds
        //其中 ParamType对应的value值为1时表示投影样式，为2时表示截面样式
        //ElementIds 对应的value值为对象的Id，支持传入多个ElementId（英文逗号隔开），不传的话内部自动去取选择集中的对象
        //该示例代码不传ElementIds，修改选中的填充对象投影样式
        gcmp::CommandParameters commandParams;
        commandParams.emplace(L"ParamType", 1);
        gcmp::ICommandManager* pCommandManager = gcmp::ICommandManager::Get();
        DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pCommandManager, L"pCommandManager为空",L"GDMPLab",L"2024-03-30");
        pCommandManager->SendCommand(gdmp::CommandId::SetElementGraphicsStyle, commandParams);
        return nullptr;
    }

    gcmp::IDocument* ProjectionStyleCommand::GetDocument() const
    {
        gcmp::IUiDocumentViewManager* pUiDocumentViewManager = IUiDocumentViewManager::Get();;
        if (pUiDocumentViewManager) {
            gcmp::IUiDocument* pUiDocument = pUiDocumentViewManager->GetCurrentUiDocument();
            if (pUiDocument) {
                gcmp::IDocument* pDocument = pUiDocument->GetDbDocument();
                if (pDocument) {
                    if (!pDocument->IsFamilyDocument()) {
                        return pDocument;
                    }
                }
            }
        }
        return nullptr;
    }

    namespace
    {
        //仅作为示例展示使用，注册命令,当析构时移除注册
        gdmp::Command stub{ NEW_AS_OWNER_PTR(ProjectionStyleCommand) };
    }
}
